﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Resto.CostControl.Core.Domain.Model;
using Resto.CostControl.Core.Cache;
using Resto.CostControl.Core.Facade.MaterialModule.DataTransferObject;
using Resto.CostControl.Core.Enums;

namespace Resto.CostControl.Core.Facade.SupplierModule.DataTransferObject
{
    /// <summary>
    /// 原材料采购单DTO
    /// </summary>
    [Serializable]
    public class MaterialPurchaseDTO
    {
        #region 构造函数

        public MaterialPurchaseDTO() { }

        internal MaterialPurchaseDTO(MaterialPurchase order)
        {
            this.ID = order.ID;
            this.Code = order.Code;
            this.OrderDate = order.OrderDate;
            this.SupplierId = order.SupplierID;
            this.OrderState = (MaterialPurchaseState)Enum.Parse(typeof(MaterialPurchaseState),order.OrderState);

            List<MaterialPurchaseItemDTO> list = new List<MaterialPurchaseItemDTO>();
            foreach (MaterialPurchaseItem item in order.Items)
                list.Add(new MaterialPurchaseItemDTO(item));

            this.Items = list.ToArray();

        }

        #endregion

        #region 基本属性

        public string ID { get; set; }

        /// <summary>
        /// 合同编号
        /// </summary>
        public string Code { get; set; }

        /// <summary>
        /// 供应商Id
        /// </summary>
        public string SupplierId { get; set; }

        public string SupplierName
        {
            get
            {
                SupplierDTO supplierDTO = SupplierDTOCache.Get(SupplierId);
                if (supplierDTO != null)
                    return supplierDTO.Name;
                else
                    return string.Empty;
            }
        }


        /// <summary>
        /// 订购日期
        /// </summary>
        public DateTime OrderDate { get; set; }

        /// <summary>
        /// 价格汇总
        /// </summary>
        public decimal TotalPrice { get; set; }

        public MaterialPurchaseItemDTO[] Items { get; set; }

        /// <summary>
        /// 采购单状态,UI不要赋值
        /// </summary>
        public MaterialPurchaseState OrderState { get; set; }

        #endregion

        #region 方法

        internal MaterialPurchase ToMaterialPurchase()
        {
            MaterialPurchase order = new MaterialPurchase();
            CopyToMaterialPurchase(order);
            return order;
        }

        internal void CopyToMaterialPurchase(MaterialPurchase order)
        {
            order.Code = this.Code;
            order.SupplierID = this.SupplierId;
            order.OrderDate = this.OrderDate.Date;
            order.TotalPrice = this.TotalPrice;

            //清除要删除的项
            string[] oldIds = (from o in order.Items
                            select o.ID).ToArray();

            string[] newIds = (from o in this.Items
                            select o.ID).ToArray();

            List<string> delIds = (from o in oldIds
                            where !newIds.Contains(o)
                            select o).ToList();

            delIds.ForEach(x => order.RemoveItem(x));


            //修改和新增项
            foreach (MaterialPurchaseItemDTO itemDTO in Items)
            {
                MaterialPurchaseItem item;
                if (!string.IsNullOrWhiteSpace(itemDTO.ID))
                {
                    item = order.GetItem(itemDTO.ID);
                    if (item != null)
                        itemDTO.CopyToMaterialPurchaseItem(item);
                }
                else
                    order.AddItem(itemDTO.ToMaterialPurchaseItem());
            }
        }

        #endregion
    }
}
